<?php

class Users extends Eloquent {
    protected $table = 'users';
    public static function Select($shop_id)
    {
        try{

            $query_select="SELECT *  FROM users where shop_id = $shop_id ";
            return DB::select(DB::raw($query_select));
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    //insert to data
     public static function Insert($name,$address,$birthdate,$phone,$email,$people_id,$salary,$shop_id,$username,$password,$comment)
    {
      try{
            $users= new Users();
            $users->name = $name;
            $users->address =$address;
            $users->birthdate=$birthdate;
            $users->phone=$phone;
            $users->email=$email;
            $users->people_id=$people_id;
            $users->salary=$salary;
            $users->shop_id = $shop_id;
            $users->comment = $comment;
            $users->save();
            $staffid = $users->id;
            $acc= Accounts::where('user_id',$staffid)->get()->first();
            if($acc){
                $acc = new Accounts();
                $acc->user_id = $staffid;
                $acc->username =$username;
                $acc->password = $password;
                $acc->save();
                return $staffid;
            }else{
                        return json_encode(array('error'=>'insert fail'));
            }
          }
        catch(Exception $e)
        {
            throw $e;
        }
    }

       
    public static function Edit($name,$address,$birthdate,$phone,$email,$people_id,$salary,$staff_id)
    {
        //$user = Users::where('name',$name)->get()->first();
        //$user = Users::find($staff_id);
        //ser->name = $name;
     // $user->save();

        try
        {
            $query_edit =" UPDATE users 
                           SET updated_at=CURRENT_TIMESTAMP,name = '$name',address = '$address',birthdate='$birthdate' ,phone=$phone,email = '$email',people_id='$people_id',salary='$salary'
                           WHERE id = $staff_id";
            $query= DB::update($query_edit);
            return true;
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    public static function Deletedata($staff_id){
        try{
            $query_delete ="DELETE FROM users WHERE id = '$staff_id'";
            $delete=DB::delete($query_delete);
            return true;
        }
        catch(Exception $e){
            throw $e;
        }
    }
    public static function ChangePassword($password_old,$password_new,$staff_id)
    {
          // allow to change password;
        try{
        $pass=($_POST['password_old']);
        $result="SELECT password FROM accounts WHERE user_id = $staff_id AND password='$pass'";
        $result_r= DB::select($result);  
          //SELECT `password` FROM accounts WHERE (`user_id` = '1' AND `password`='123')
          if ($result_r==true) {
                $query_edit =" UPDATE accounts 
                           SET updated_at=CURRENT_TIMESTAMP,password = '$password_new'
                           WHERE  user_id = '$staff_id'";
            $query= DB::update($query_edit);
            return true;               
          }
          else
          {
                throw new Exception('old password wrong!');
          }
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }

}
